<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Agendamentos extends CI_Controller
{
	private $table = "agenda";
	private $pk = "id";
	
	function __construct()
	{
		parent::__construct();
		if (!$this->session->userdata('loggedin'))
		{
			redirect(base_url(), 'refresh');
		}
	}
	
	function index()
	{
		$this->load->view('common/header');
		$this->load->view('common/menu');
		$this->load->view('agendamentos');
		$this->load->view('common/footer');
	}
    
	function listAll()
	{
	    parse_str($_SERVER['QUERY_STRING'],$_GET);
	    $objSearch = null;
		
		$objSearch->groupOp = "AND";
		$objSearch->rules[0]->field = "tb_agenda.data_consulta";
		$objSearch->rules[0]->op = "le";
		$objSearch->rules[0]->data = date('Y-m-d');
		
		if(isset($_REQUEST['filters']) && !is_null($_REQUEST['filters']) && !empty($_REQUEST['filters']))
		{
			$objSearch = json_decode(stripslashes($_REQUEST['filters']));
		}

		$data['page'] = isset($_GET['page'])?$_GET['page']:1;
		$data['rows'] = isset($_GET['rows'])?$_GET['rows']:10;
		$data['sidx'] = isset($_GET['sidx'])?$_GET['sidx']:'tb_agenda.data_consulta';
		$data['sord'] = isset($_GET['sord'])?$_GET['sord']:'ASC';
		
		$this->load->model("agenda_model");
		$dados['agenda'] = $this->agenda_model->getByFilterPaging($objSearch, $this->table, $data);
		$i = 0;
		$responce = null;
		$responce->page = $dados['agenda']['page'];
		$responce->total = $dados['agenda']['totalpages'];
		$responce->records = $dados['agenda']['totalrecords'];

		foreach ($dados['agenda']['result'] as $row)
		{
			$responce->rows[$i]['id']=$row->id;
        	$responce->rows[$i]['cell']=array($row->id, $row->especialidade, $row->medico, $row->paciente, $row->data_consulta, $row->horario, $row->flag_paciente_confirmado);
        	$i++;
		}
		echo json_encode($responce);
	}

	function confirm()
	{
		$data['id'] = $this->input->post('id');
		$data['flag_paciente_confirmado'] = "S";
		if($this->generic_model->update($data, $this->table, $this->pk))
		{
			$this->index();
			echo "oks#Agendamento confirmado com sucesso!";
		}
		else
		{
			echo "err#Erro na confirma&ccedil;&atilde;o do Agendamento";
		}
		exit;
	}
}